home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4868 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.5 KB

  1. Path: news.lpr.carel.fi!usenet
  2. From: Ari Lukumies <aril@cmt.lpr.mail.carel.fi>
  3. Newsgroups: comp.lang.c,comp.lang.c++
  4. Subject: Re: New printf - like function, is it possible?
  5. Date: Thu, 01 Feb 1996 14:40:24 +0200
  6. Organization: Carelcomp Forest
  7. Message-ID: <3110B4B8.5634@cmt.lpr.mail.carel.fi>
  8. References: <4eqb3a$2r5@pan.otol.fi>
  9. NNTP-Posting-Host: renoir.cclahti.carel.fi
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0b6a (WinNT; I)
  14.  
  15. Timo Sakari wrote:
  16. > Hi!
  17. > Do you C-gurus have any solutions to this problem?
  18. > I am programming under MS Windows 3.1 (this is NOT an OS spesific
  19. > question!)and I would like to create
  20. > some kind of modified print function, which syntax should be something
  21. > like normal printf; because in Windows printf can't be used, everything
  22. > printed to screen has to come through message boxes etc, and before
  23. > displaying anything, the string to displayed has to be formed. I have
  24. > created my own printing function, which wants parameters string and winID
  25. > to know where to put that string, something like this:
  26. > sprintf(printStr, "some sfuff, variables %d, %f, etc", var1, var2);
  27. > PrintToWin(DEBUG_WIN_1, printStr);
  28. > However, I want to write a "better" printing function, where string to be
  29. > formed doesn't need to be formatted first, it can be displayed and formed
  30. > at the same time in the same syntax like printf, something like these:
  31. > PrintToWin(DEBUG_WIN_1, "some stuff etc, %d, %f, %s, var1, var2, str1);
  32. > PrintToWin(OTHER_WIN, "anything that printf accepts");
  33. > So I want to print almost anything with this function without having
  34. > to write many functions to different argument types/number of arguments.
  35. > This should be possible in C++ (is it?), but is it possible in C ?
  36. > Can this be achieved by using void pointers and macros etc., or should
  37. > I give up and don't waste my time on this?
  38. > Any help would be greatly appreciated. Thanks.
  39.  
  40. You could try:
  41.  
  42.     #include    <stdarg.h>
  43.  
  44.     void    MyPrintf(HDC hDC, int x, int y, char *fmt, ...)
  45.     {
  46.         va_list    argp;
  47.         char    tmp[long_enough_for_your_needs];
  48.  
  49.         va_start(argp, fmt);
  50.         vsprintf(tmp, fmt, argp);
  51.         TextOut(hDC, x, y, tmp, strlen(tmp));
  52.         va_end(argp);
  53.     }
  54.  
  55. Alternatively, if you'd want TTY like output, you can GetTextMetrics for the font you use and 
  56. use the metrics to increment x and y as required (taking into account newlines etc) so you 
  57. won't have to supply them as parameters each time.
  58.  
  59. HTH,
  60.  AriL
  61. -- 
  62. All my opinions are mine and mine alone.
  63.